/**************************************************************************************************
 * light library                                                                                  *
 * Copyright © 2012 David Kretzmer                                                                *
 *                                                                                                *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software  *
 * and associated documentation files (the "Software"), to deal in the Software without           *
 * restriction,including without limitation the rights to use, copy, modify, merge, publish,      *
 * distribute,sublicense, and/or sell copies of the Software, and to permit persons to whom the   *
 * Software is furnished to do so, subject to the following conditions:                           *
 *                                                                                                *
 * The above copyright notice and this permission notice shall be included in all copies or       *
 * substantial portions of the Software.                                                          *
 *                                                                                                *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING  *
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND     *
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.        *
 *                                                                                                *
 *************************************************************************************************/

#include "light/io/file.hpp"

#ifdef LIGHT_PLATFORM_LINUX
	#include "light/io/internal/linux_file.hpp"
#else
	#error Unsupported platform
#endif


namespace light
{
namespace io
{
	// Constructor. Opens a file
	file::file(utf8_string const &filename, access acc) :
		m_impl(new internal::file_base(filename.c_str(), acc))
	{

	}

	// Destructor. Closes the file
	file::~file()
	{

	}

	// Opens a file. If you are only reading the file then it must already exist
	void file::open(utf8_string const &filename, access acc)
	{
		m_impl->open(filename.c_str(), acc);
	}

	// Closes the file
	void file::close()
	{
		m_impl->close();
	}

	// Writes 'num_bytes' from 'data' to the file. Returns the number of bytes actually written
	size_t file::write(void const *data, size_t num_bytes)
	{
		return m_impl->write(data, num_bytes);
	}

	// Reads 'num_bytes' from the file to 'data'. Returns the number of bytes actually read
	size_t file::read(void *data, size_t num_bytes)
	{
		return m_impl->read(data, num_bytes);
	}

	// Sets the read/write offset relative from 'pos'. Returns the offset from tzhe beginning of the file
	uint8 file::seek(uint8 offset, seek_pos pos)
	{
		return m_impl->seek(offset, pos);
	}

	// Gets the current read/write offset
	uint8 file::tell() const
	{
		return m_impl->tell();
	}

	// Gets the size of the file
	uint8 file::size() const
	{
		return m_impl->size();
	}

} // namespace: io
} // namespace: light

